package com.taotao.common.service;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.bean.HttpClientResult;

/**
 * 专门提供调用远程调用接口的工具
 * 
 * @author Beck
 *
 */
@Service
public class HttpClientService {
    @Autowired(required=false)
    private CloseableHttpClient httpClient;

    /**
     * httpClient发送无参数的请求
     * 
     * @param url
     * @return 返回的内容
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doGet(String url) throws ClientProtocolException, IOException {
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(url);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = this.httpClient.execute(httpGet);
            // 判断返回状态是否为200 报文 响应状态码
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                return content;
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }

    /**
     * httpClient发送有参的get请求
     * 
     * @param url
     * @param params 参数(Map)
     * @return String
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
    public String doGet(String url, Map<String, String> params)
            throws ClientProtocolException, IOException, URISyntaxException {
        // 设置url
        URIBuilder uriBuilder = new URIBuilder(url);
        // 设置参数
        for (String key : params.keySet()) {
            uriBuilder.setParameter(key, params.get(key));
        }
        // build方法创建出URI对象
        URI uri = uriBuilder.build();

        // 创建http GET请求
        HttpGet httpGet = new HttpGet(uri);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = this.httpClient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                return content;
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }

    /**
     * httpClient发送无参数的post请求
     * 
     * @param url
     * @return HttpClientResult
     * @throws ClientProtocolException
     * @throws IOException
     */
    public HttpClientResult doPost(String url) throws ClientProtocolException, IOException {
        // 创建http POST请求 输入URL
        HttpPost httpPost = new HttpPost(url);
        // 模拟了浏览器来进行访问
        httpPost.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");

        CloseableHttpResponse response = null;
        try {
            // 执行请求 敲回车
            response = this.httpClient.execute(httpPost);
            // 请求返回的状态码
            int statusCode = response.getStatusLine().getStatusCode();
            // 返回的内容
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            return new HttpClientResult(statusCode, content);
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }

    public HttpClientResult doPost(String url, Map<String, String> params)
            throws ClientProtocolException, IOException {
        // 创建http POST请求
        HttpPost httpPost = new HttpPost(url);

        // 设置参数
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        for (String key : params.keySet()) {
            parameters.add(new BasicNameValuePair(key, params.get(key)));
        }

        // 构造一个form表单式的实体
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 将请求实体设置到httpPost对象中 报文
        httpPost.setEntity(formEntity);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = this.httpClient.execute(httpPost);
            // 请求返回的状态码
            int statusCode = response.getStatusLine().getStatusCode();
            // 返回的内容
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            return new HttpClientResult(statusCode, content);
        } finally {
            if (response != null) {
                response.close();
            }
        }

    }

}
